//
//  GHSStockFilterView.m
//  penzer
//
//  Created by 还带大道 on 2018/11/22.
//  Copyright © 2018年 caiyc. All rights reserved.
//

#import "GHSStockFilterView.h"

@interface GHSStockFilterView()
@property (weak, nonatomic) IBOutlet UIView *bgV;
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (strong, nonatomic) NSMutableArray<UIButton *> *btn_items;
@property (nonatomic,strong) UIView *headerView;

@end

@implementation GHSStockFilterView


+ (instancetype)shareView
{
    return [[NSBundle mainBundle] loadNibNamed:NSStringFromClass([self class]) owner:nil options:nil].lastObject;
}


- (void)awakeFromNib {
    [super awakeFromNib];
    
    self.backgroundColor = [UIColor clearColor];
    self.bgV.backgroundColor = RZ_COLOR_ALPHA(0, 0, 0, 0.7);
    self.tableView.backgroundColor = [UIColor clearColor];
    self.tableView.bounces = NO;
    //添加item
    [self addItems:@[@"全部",@"手机",@"PAD",@"Watch",@"iphone"]];
    
}



- (void)layoutSubviews
{
    [super layoutSubviews];
    NSInteger maxcount = 4;
    CGFloat margin_x = 15;
    CGFloat margin_y = 15;
    CGFloat height = 25;
    CGFloat width = (self.width - margin_x*(maxcount + 1)) / maxcount ;
    __block CGFloat x = 0;
    __block CGFloat y = 0;
    __block NSInteger row = 0;
    __block NSInteger col = 0;

    [self.btn_items enumerateObjectsUsingBlock:^(UIButton * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        
        col = idx % maxcount;
        row = idx / maxcount;
        x = margin_x + (width + margin_x)*col;
        y = margin_y + (height + margin_y)*row;
        obj.frame = CGRectMake(x, y, width, height);
        obj.layer.cornerRadius = 12.5;
        
    }];
    self.tableView.tableHeaderView.height = CGRectGetMaxY(self.btn_items.lastObject.frame) + 15;
    
}




- (void)addItems:(NSArray<NSString *> *)items
{
    if(items.count == 0) return;
    UIView *headerV = [UIView new];
    headerV.backgroundColor = RZ_COLOR(60, 34, 95);
    self.tableView.tableHeaderView = headerV;
    [items enumerateObjectsUsingBlock:^(NSString * _Nonnull str, NSUInteger idx, BOOL * _Nonnull stop) {
        UIButton *btn = [UIButton new];
        btn.tag = idx;
        [headerV addSubview:btn];
        [self.btn_items addObject:btn];
        [btn addTarget:self action:@selector(itemOnClick:) forControlEvents:UIControlEventTouchUpInside];
        [btn setTitle:str forState:UIControlStateNormal];
        [btn setTitleColor:RZ_COLOR(60, 34, 95) forState:UIControlStateNormal];
        btn.titleLabel.font = [UIFont fontWithName:@"PingFangSC-Semibold" size:15];
        btn.backgroundColor = [UIColor whiteColor];
    }];
    [self layoutIfNeeded];
}




- (void)itemOnClick:(UIButton *)btn
{
    !self.itemOnClickBlock ? :self.itemOnClickBlock(btn.tag);
}



#pragma mark--getter&&setter
- (NSMutableArray<UIButton *> *)btn_items
{
    if(!_btn_items){
        _btn_items = [NSMutableArray array];
    }
    return _btn_items;
}

@end
